﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using FineUI;

namespace lcmyjobs.Web.enprise
{
    public partial class ModifyJobs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Application["webname"] != null)
                {
                    this.Title = string.Format("单位管理中心—职位管理：职位查看、修改  {0}", Application["webname"].ToString());
                }
                if (Session["enterprise_rid"] != null)
                {
                    if (Session["enterprise_name"] != null)
                    {
                        this.lbl_title.Text = " " + Session["enterprise_name"].ToString();
                    }

                    int enterprise_rid = Convert.ToInt32(Session["enterprise_rid"]);
                    ShowInfo(enterprise_rid);
                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登陆！", "EnterpriseLogin.html");
                }
            }
        }

        //保存修改
        protected void btn_upjobs_Click(object sender, EventArgs e)
        {
            if (Session["enterprise_rid"] != null && Session["jobsCount"] != null)
            {
                int enterprise_rid = Convert.ToInt32(Session["enterprise_rid"]);

                lcmyjobs.Model.jobsCount jc = Session["jobsCount"] as lcmyjobs.Model.jobsCount;

                //string reardpay = this.txtrewardpay.Text;

                //Regex r = new Regex(@"^\d+(\.)?\d*$");
                //if (r.IsMatch(reardpay))
                //{
                    #region
                    if (this.hid_isFrist.Value != "1")//如果所修改的职位不是急聘职位
                    {
                        if (this.cbo_isfrist.Checked)//如果修改为急聘职位
                        {
                            #region 由非急聘职位转变到急聘职位的会员权限计算
                            if (jc.fristJobCount - jc.isFristJobsCount > 0)//判断会员权限是否可以发布此急聘职位
                            {
                                if (Save(enterprise_rid))
                                {
                                    jc.isFristJobsCount = jc.isFristJobsCount + 1;
                                    Session["jobsCount"] = jc;
                                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位成功！您还可以发布" + (jc.fristJobCount - jc.isFristJobsCount) + "个急聘职位.", "ListJobs.html");
                                }
                                else
                                {
                                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位失败！请重新填写保存。", "ModifyJobs.html");
                                }

                            }
                            else
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "您的会员权限所能发布的急聘职位已用完，或者您没有权限发布急聘职位！请您升级会员后再次发布。", "ListMember.html");
                            }
                            #endregion
                        }
                        else
                        {
                            if (Save(enterprise_rid))
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位成功！", "ListJobs.html");
                            }
                            else
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位失败！请重新填写保存。", "ModifyJobs.html");
                            }
                        }

                    }
                    else//如果所修改的职位是急聘职位
                    {
                        if (this.cbo_isfrist.Checked)
                        {
                            if (Save(enterprise_rid))
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位成功！", "ListJobs.html");
                            }
                            else
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位失败！请重新填写保存。", "ModifyJobs.html");
                            }
                        }
                        else
                        {
                            #region 由急聘职位修改为非急聘职位的会员权限计算
                            if (Save(enterprise_rid))
                            {
                                if (jc.isFristJobsCount > 1)
                                {
                                    jc.isFristJobsCount = jc.isFristJobsCount - 1;
                                    Session["jobsCount"] = jc;
                                }
                                
                                //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位成功！您还可以发布" + (jc.jobsTotalCount - jc.jobsPubCount) + "个职位.", "ListJobs.html");
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位成功！您还可以发布" + (jc.fristJobCount - jc.isFristJobsCount) + "个急聘职位.", "ListJobs.html");
                            }
                            else
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改职位失败！请重新填写保存。", "ModifyJobs.html");
                            }
                            #endregion
                        }
                    }
                    #endregion
                //}
                //else
                //{
                //    Maticsoft.Common.MessageBox.Show(this, "悬赏金额请输入数字！");
                //}



            }
        }

        private bool Save(int enterprise_rid)
        {

            //int enterprise_bid = int.Parse(this.txtenterprise_bid.Text);
            //string enterprise_bh = this.txtenterprise_bh.Text;
            //int jobs_id = int.Parse(this.txtjob.Text);
            string jobs_name = this.txtjob.Text;
            string jobclass = this.txtjobclass.Text;
            string hid_jobclass = this.hid_jobclass.Value;
            int workercount = int.Parse(this.txtworkercount.Text);
            string jobdep = this.txtjobdep.Text;
            string salarym = this.ddlsalarym.SelectedItem.Value;
            string salaryy = "";//年薪
            if (!string.IsNullOrEmpty(this.txtsalaryy.Text))
            {
                salaryy = this.txtsalaryy.Text;
            }

            int validity = int.Parse(this.txtvalidity.Text);
            int workerclass = -1;
            if (this.rdo_bx.Checked)
            {
                workerclass = 0;
            }
            if (this.rdo_wjs.Checked)
            {
                workerclass = 1;
            }
            if (this.rdo_yjs.Checked)
            {
                workerclass = 2;
            }

            int edus_id = int.Parse(this.ddledus_id.SelectedItem.Value);
            int workexp_id = int.Parse(this.ddlworkxp_id.SelectedItem.Value);
            string jobduty = this.txtjobduty.Text;
            string jobrequirement = this.txtjobrequirement.Text;
            DateTime releasetime = DateTime.Now;
            //bool isuse = this.chkisuse.Checked;
            int languages_id = int.Parse(this.ddllanguage.SelectedItem.Value);
            string languages_level = this.ddllanguageslevel.SelectedItem.Value;//外语水平
            //int bcount = int.Parse(this.txtbcount.Text);
            bool isfrist = this.cbo_isfrist.Checked;
            //int status = int.Parse(this.txtstatus.Text);

            string area_name1 = "0";
            string area_name2 = "0";
            string area_name3 = "0";
            string marjor_name1 = "0";
            string marjor_name2 = "0";
            string marjor_name3 = "0";

            string[] area_names = this.txtarea.Text.Split(',');

            if (area_names.Length == 0)
            {
                area_name1 = this.txtarea.Text;
            }
            if (area_names.Length == 1)
            {
                area_name1 = area_names[0];
            }
            if (area_names.Length == 2)
            {
                area_name1 = area_names[0];
                area_name2 = area_names[1];
            }
            if (area_names.Length == 3)
            {
                area_name1 = area_names[0];
                area_name2 = area_names[1];
                area_name3 = area_names[2];
            }

            string majors = "";

            if (this.cbo_bx.Checked)
            {
                majors = "专业不限";
            }
            else
            {
                majors = this.txtmajor.Text;
            }

            string[] major_names = majors.Split(',');


            if (major_names.Length == 0)
            {
                marjor_name1 = this.txtmajor.Text.Trim();
            }
            if (major_names.Length == 1)
            {
                marjor_name1 = major_names[0];
            }
            if (major_names.Length == 2)
            {
                marjor_name1 = major_names[0];
                marjor_name2 = major_names[1];
            }
            if (major_names.Length == 3)
            {
                marjor_name1 = major_names[0];
                marjor_name2 = major_names[1];
                marjor_name3 = major_names[2];
            }

            float rewardmoney = -1f;

            if (this.cbo_isreward.Checked)
            {
                rewardmoney = float.Parse(this.txtrewardpay.Text);
            }

            lcmyjobs.Model.enterprise_releasejob model = new lcmyjobs.Model.enterprise_releasejob();
            model.enterprise_rid = enterprise_rid;
            //model.enterprise_bid = enterprise_bid;
            //model.enterprise_bh = enterprise_bh;
            //model.jobs_id = jobs_id;
            model.jobs_name = jobs_name;
            model.jobclass = hid_jobclass;//工作类别父类名
            model.job_child_class = jobclass;//工作类别子类名
            model.workercount = workercount;
            model.jobdep = jobdep;
            model.salarym = salarym;
            model.salaryy = salaryy;
            model.validity = validity;
            model.workerclass = workerclass;
            model.edus_id = edus_id;
            model.workexp_id = workexp_id;
            model.jobduty = jobduty;
            model.jobrequirement = jobrequirement;
            model.releasetime = releasetime;
            //model.isuse = isuse;
            model.languages_id = languages_id;
            model.languages_level = languages_level;
            //model.bcount = bcount;
            model.isfrist = isfrist;
            //model.status = status;
            model.area_name1 = area_name1;
            model.area_name2 = area_name2;
            model.area_name3 = area_name3;
            model.marjor_name1 = marjor_name1;
            model.marjor_name2 = marjor_name2;
            model.marjor_name3 = marjor_name3;
            model.rewardmoney = rewardmoney;

            lcmyjobs.BLL.enterprise_releasejob bll = new lcmyjobs.BLL.enterprise_releasejob();

            return bll.Update(model);

        }


        private void ShowInfo(int enterprise_rid)
        {
            lcmyjobs.BLL.enterprise_releasejob bll = new lcmyjobs.BLL.enterprise_releasejob();
            DataSet ds = bll.GetReleaseJob(enterprise_rid);

            DataTable dt_rj = ds.Tables[0];
            DataTable dt_area = ds.Tables[1];
            DataTable dt_major = ds.Tables[2];


            //this.lblenterprise_rid.Text = model.enterprise_rid.ToString();
            //this.txtenterprise_bid.Text = model.enterprise_bid.ToString();
            //this.txtenterprise_bh.Text = model.enterprise_bh;
            //this.txtjobs_id.Text = model.jobs_id.ToString();
            if (dt_rj.Rows.Count > 0)
            {
                this.txtjobclass.Text = dt_rj.Rows[0]["job_name"].ToString();
                this.hid_jobclass.Value = dt_rj.Rows[0]["jobclass"].ToString();
                this.txtworkercount.Text = dt_rj.Rows[0]["workercount"].ToString();
                if (dt_rj.Rows[0]["jobdep"].ToString() == "0")
                {
                    this.txtjobdep.Text = "面谈";
                }
                else
                {
                    this.txtjobdep.Text = dt_rj.Rows[0]["jobdep"].ToString();
                }

                this.txtjob.Text = dt_rj.Rows[0]["jobs_name"].ToString();
                this.ddlsalarym.SelectedValue = dt_rj.Rows[0]["salarym"].ToString();

                if (!string.IsNullOrEmpty(dt_rj.Rows[0]["salaryy"].ToString()))//年薪
                {
                    this.cbo_isnx.Checked = true;
                    this.txtsalaryy.Text = dt_rj.Rows[0]["salaryy"].ToString();
                }


                this.txtvalidity.Text = dt_rj.Rows[0]["validity"].ToString();

                if (dt_rj.Rows[0]["workerclass"].ToString() == "0")
                {
                    this.rdo_bx.Checked = true;
                }
                if (dt_rj.Rows[0]["workerclass"].ToString() == "1")
                {
                    this.rdo_wjs.Checked = true;
                }
                if (dt_rj.Rows[0]["workerclass"].ToString() == "2")
                {
                    this.rdo_yjs.Checked = true;
                }

                this.ddledus_id.SelectedValue = dt_rj.Rows[0]["edus_id"].ToString();
                this.ddlworkxp_id.SelectedValue = dt_rj.Rows[0]["workexp_id"].ToString();
                this.txtjobduty.Text = dt_rj.Rows[0]["jobduty"].ToString();
                this.txtjobrequirement.Text = dt_rj.Rows[0]["jobrequirement"].ToString();
                //this.txtreleasetime.Text = model.releasetime.ToString();
                //this.chkisuse.Checked = model.isuse;
                this.ddllanguage.SelectedValue = dt_rj.Rows[0]["languages_id"].ToString();
                this.ddllanguageslevel.SelectedValue = dt_rj.Rows[0]["languages_level"].ToString();
                //this.txtbcount.Text = model.bcount.ToString();
                if (dt_rj.Rows[0]["isfrist"].ToString() == "True")
                {
                    this.cbo_isfrist.Checked = true;
                    this.hid_isFrist.Value = "1";//记录此职位是否为急聘职位，用于在保存时计算权限
                }
                else
                {
                    this.hid_isFrist.Value = "0";
                }
                if (!string.IsNullOrEmpty(dt_rj.Rows[0]["price"].ToString()))//如果悬赏金额不为空
                {
                    this.cbo_isreward.Checked = true;
                    this.txtrewardpay.Text = dt_rj.Rows[0]["price"].ToString();
                }
            }

            if (dt_area.Rows.Count > 0)
            {
                string area_name = "";
                foreach (DataRow row in dt_area.Rows)
                {
                    area_name += row["area_name"].ToString() + ",";

                }

                this.txtarea.Text = area_name.Substring(0, area_name.Length - 1);
            }

            if (dt_major.Rows.Count > 0)
            {
                string major_name = "";
                foreach (DataRow row in dt_major.Rows)
                {
                    major_name += row["major_name"].ToString() + ",";

                }

                this.txtmajor.Text = major_name.Substring(0, major_name.Length - 1);
            }
            else
            {
                this.cbo_bx.Checked = true;
                this.txtmajor.Text = "专业不限";
            }
        }

        protected void btn_why_Click(object sender, EventArgs e)
        {
            /*
            Alert alert = new Alert();
            string msg = "<div style='width:250px;text-indent:2em; line-height:20px;'><p style='padding-Bottom:5px; '>  悬赏招聘：更准确、更快捷、更低成本。</p>";
            msg += "<p style='padding-Bottom:5px; '>  更准确----提供专业的人才选择工具，帮助企业深度甄选人才。 基于人才胜任素质的评测工具辅助人才选择。颠覆被动“等待”人才的传统方式。</p>";
            msg += "<p style='padding-Bottom:5px; '>  更快捷----发挥社会力量，动员群众，借助猎手人脉资源关系，让您的悬赏信息迅速覆盖目标群体，快速找到适合人才。</p>";
            msg += "<p>  更低成本----悬赏招聘使企业能够以较低的价格完成猎头服务。悬赏企业自行定价、发布有效职位，成本仅为传统猎头费用(费用为该职位年薪的30%)的十分之一，甚至更低，并全程享受专业人事招聘服务，轻松猎才。</p></div>";


            alert.Message = msg;
            alert.Icon = Icon.Help;
            
            alert.Title = "为什么要悬赏招聘";
            alert.Show();
             */


            string msg = "<div style='width:250px;text-indent:2em; line-height:20px;'><p style='padding-Bottom:5px; '>  悬赏招聘：更准确、更快捷、更低成本。</p>";
            msg += "<p style='padding-Bottom:5px; '>  更准确----提供专业的人才选择工具，帮助企业深度甄选人才。 基于人才胜任素质的评测工具辅助人才选择。颠覆被动“等待”人才的传统方式。</p>";
            msg += "<p style='padding-Bottom:5px; '>  更快捷----发挥社会力量，动员群众，借助猎手人脉资源关系，让您的悬赏信息迅速覆盖目标群体，快速找到适合人才。</p>";
            msg += "<p>  更低成本----悬赏招聘使企业能够以较低的价格完成猎头服务。悬赏企业自行定价、发布有效职位，成本仅为传统猎头费用(费用为该职位年薪的30%)的十分之一，甚至更低，并全程享受专业人事招聘服务，轻松猎才。</p></div>";
            //ClientScript.RegisterStartupScript(this.GetType(), "why", "alert(" + msg + ")", true);
            //ClientScript.RegisterStartupScript(this.GetType(), "a", "if(confirm('您还没有登录！是否要登录？'))window.location='../users/userlogin.html'", true);
            Alert.Show(msg);

        }
    }
}